package com.example.rental.controller;
import com.example.rental.entity.User;
import com.example.rental.entity.condition.UserCondition;
import com.example.rental.entity.dto.RegistDto;
import com.example.rental.entity.dto.UserDto;
import com.example.rental.result.Result;
import com.example.rental.service.OderService;
import com.example.rental.service.UserService;
import com.example.rental.util.WebUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(tags = "用户操作接口文档",description = "登录,注册，查看用户,搜索用户，修改用户")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OderService oderService;

    @GetMapping("/select")
    public List<User> select(){
        return userService.selectAll();
    }
    @PostMapping("/search")
    //@RequestBody 以json形式接收数据
    public Result<PageInfo<User>> searchUsers(@RequestBody UserCondition userCondition){
        System.out.println(userCondition);
        return Result.ok(userService.searchUsers(userCondition));
    }
    @GetMapping("/del")
    public Result deleteUser(@RequestParam("userid") String userid){
        //查询用户的订单状态
        List<Integer> statusList = oderService.selectStatusByUserId(Integer.parseInt(userid));
        for (Integer integer : statusList) {
            if(integer == 1){//有订单正在进行
                return Result.error(200,"有订单正在进行");
            }
        }
        userService.deleteUserByUserid(userid);

        return Result.ok("删除成功");
    }
    @PostMapping("/add")
    public Result add(@RequestBody User user){
        System.out.println(user);
        if(userService.findByPhone(user.getPhone()) != null){
            return Result.error(200,"该手机号已经被注册");
        }
        userService.AddUser(user); 
        return Result.ok(user);
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        System.out.println(user);
        userService.EditUser(user);
        return Result.ok(user);
    }
    @PostMapping("/login")
    public Map userLogin(@RequestBody UserDto userDto){
        User user =new User();
        Map map=new HashMap();
        if(userDto.getPassword() != "" || userDto.getPassword() != null && userDto.getPhone() != "" || userDto.getPhone() != null) {
            user=userService.userLogin(userDto.getPhone(), userDto.getPassword());

            if (user!=null){
                if(user.getState() != 0)
                {
                    map.put("msg",user);
                    return map;
                }else {
                    map.put("msg","冻结账号！");
                    return map;}
            }
        }
        map.put("msg","账号或密码错误！");
        return map;
    }
    @PostMapping("/insert")
    public Result newUser(@Validated @RequestBody RegistDto registDto, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return WebUtils.getErrors(bindingResult);
        }
        if(userService.findByPhone(registDto.getPhone()) != null){
            return Result.error(200,"该手机号已经被注册");
        }

        userService.newUser(registDto.getPhone(),registDto.getPassword());

        return Result.ok();

    }
}
